package com.qsbc.pygram.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qsbc.pygram.common.Result;
import com.qsbc.pygram.entity.Checkpoint;
import com.qsbc.pygram.entity.CheckpointContent;
import com.qsbc.pygram.service.CheckpointContentService;
import com.qsbc.pygram.service.CheckpointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2023-02-13
 */
@RestController
@RequestMapping("/pygram/checkpoint-content")
public class CheckpointContentController {

    @Autowired
    private CheckpointContentService checkpointContentService;

    //    查所有
    @GetMapping
    public List<CheckpointContent> findlist() {
        return checkpointContentService.list(null);
    }

    //    增加
    @PostMapping
    public Result save(@RequestBody CheckpointContent checkpointContent) {
        List<Integer> exist = checkpointContentService.findByid(checkpointContent);

        int i =exist.size();

        if (checkpointContent.getContentId() == null && i>0) {

            return Result.error();
        }
        return Result.success(checkpointContentService.saveOrUpdate(checkpointContent));
    }


    //    根据id删除
    @DeleteMapping("{contentId}")
    public Result delete(@PathVariable String contentId) {
        return Result.success(checkpointContentService.removeById(contentId));
    }


    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<String> contentIds) {
        return Result.success(checkpointContentService.removeByIds(contentIds));
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") Integer  checkpointId,
                           @RequestParam(defaultValue = "") String contentType) {

        return Result.success(checkpointContentService.findPage(new Page<>(pageNum, pageSize), checkpointId,contentType));
    }

}
